<?php

namespace addons\config\model;

/**
 * 系统行情
 */
class Quotation extends \web\common\model\BaseModel{
    
    protected function _initialize(){
        $this->tableName = 'sys_quotation';
    }
	
	public function getQuotationList($map,$field,$page,$size,$order = 'id desc'){
		return $this->field($field)->where($map)->limit($page,$size)->order($order)->select();
	}


	/**
     * 外网行情
     */
    public function getCoinInformation(){
        $redis = \think\Cache::connect(\think\Config::get('global_cache'));
        // $arr = $redis->get('hotapi_price');
        // if(!$arr){
            $price = 1;
            $HotApi = new \web\common\utils\HotApi();
            $list = ['ethusdt'];
            $arr = [];
            foreach ($list as $key => $value) {
                $info = $HotApi->get_detail_merged($value);
                if($info['success']&&$info['data']['status']=='ok'){
                    $tmp['price'] = bcmul($price, $info['data']['tick']['close'],2);
                    $tmp['difference'] = bcmul($price, ($info['data']['tick']['close']-$info['data']['tick']['open']),2);
                    if($info['data']['tick']['close']-$info['data']['tick']['open']>0){
                        $tmp['type'] = 1;
                    }else{
                        $tmp['type'] = 0;
                    }
                    if($value=='ethusdt'){
                        $tmp['name'] = 'ETH';
                    }
                    $arr[] = $tmp;
                }
            }
            // $tmp['type'] = 1;
            // $tmp['price'] = $price;
            // $tmp['difference'] = 0;
            // $tmp['name'] = 'USDT';
            // $arr[] = $tmp;
            $redis->set('hotapi_price', $arr, 60);
        // }
        return $arr;
    }

    /**
     * 获取价格
     */
    public function getPrice(){
        $redis = \think\Cache::connect(\think\Config::get('global_cache'));
        $new = $redis->get('quotation_price');
        if(!$new){
            $new['eth'] = 169.66;
            // $arr = $this->getCoinInformation();
            // $new = [];
            // foreach ($arr as $key => $v) {
            //     if($v['name']=='ETH'){
            //         $new['eth'] = $v['price'];
            //     }
            // }
            // $redis->set('quotation_price', $new, 60);
        }
        return $new;
    }

    public function getNowPrice(){
        return $this->order('id desc')->value('price_now');
    }
}
